package client;

import com.alibaba.fastjson.JSONObject;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

//import org.apache.log4j.Logger;


public class TCPClient {
//	private static final Logger logger = Logger.getLogger(TcpClient.class);
	public static String HOST = "127.0.0.1";
	public static int PORT = 9999;
	
	public static Bootstrap bootstrap = getBootstrap();
	public static Channel channel = getChannel(HOST,PORT);
	/**
	 * 初始化Bootstrap
	 * @return
	 */
	public static final Bootstrap getBootstrap(){
		EventLoopGroup group = new NioEventLoopGroup();
		Bootstrap b = new Bootstrap();
		b.group(group).channel(NioSocketChannel.class);
		b.handler(new ChannelInitializer<Channel>() {
			@Override
			protected void initChannel(Channel ch) throws Exception {
				ChannelPipeline pipeline = ch.pipeline();
				pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
				pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
				pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
				pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
				pipeline.addLast("handler", new TCPClientHandler());
			}
		});
		b.option(ChannelOption.SO_KEEPALIVE, true);
		return b;
	}

	public static final Channel getChannel(String host,int port){
		Channel channel = null;
		try {
			channel = bootstrap.connect(host, port).sync().channel();
		} catch (Exception e) {
//			logger.error(String.format("连接Server(IP[%s],PORT[%s])失败", host,port),e);
			return null;
		}
		return channel;
	}

	public static void sendMsg(String msg) throws Exception {
		if(channel!=null){
			channel.writeAndFlush(msg).sync();
		}else{
			System.out.println("消息发送失败,连接尚未建立!");
		}
	}

    public static void main(String[] args) throws Exception {
		try {
			long t0 = System.nanoTime();
			String msg ;
			for (int i = 0; i < 1000000; i++) {
				if(i % 3 == 0){
					msg = getMsg();
				}else{
					msg = getMsg1();
				}
				TCPClient.sendMsg(msg);
			}
			long t1 = System.nanoTime();
			System.out.println((t1-t0)/1000000.0);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public static String getMsg(){
    	JSONObject json = new JSONObject();
    	JSONObject para = new JSONObject();
    	para.put("mac", "aa:bb:cc:dd:11:22:33");
    	json.put("para",para.toString());
    	json.put("func","register");
    	return json.toString();
    }
    
    public static String getMsg1(){
    	JSONObject json = new JSONObject();
    	JSONObject para = new JSONObject();
    	para.put("mac", "aa:bb:cc:dd:11:22:33");
    	para.put("other", "ceshi");
    	json.put("para",para.toString());
    	json.put("func","register");
    	return json.toString();
    }
    
    
}